package puzzle.nurikabe.solver;

import puzzle.common.data.HatanException;
import puzzle.nurikabe.data.NurikabeBoard;
import puzzle.nurikabe.data.StateUtil;

class AvoidCrusterRule extends NurikabeCellRule {

	@Override
	protected boolean apply(NurikabeBoard b, int x, int y) throws HatanException {
		if (x == 0 || y == 0) {
			return false;
		}
		
		int t = 0;
		t |= StateUtil.isRealKabe(b.getBoardState(x, y)) ? 1 : 0;
		t |= StateUtil.isRealKabe(b.getBoardState(x - 1, y)) ? 2 : 0;
		t |= StateUtil.isRealKabe(b.getBoardState(x, y - 1)) ? 4 : 0;
		t |= StateUtil.isRealKabe(b.getBoardState(x - 1, y - 1)) ? 8 : 0;

		boolean isChanged = false;
		switch (t) {
		case 0x0e:
			isChanged |= b.setShima(x, y);
			break;
		case 0x0d:
			isChanged |= b.setShima(x - 1, y);
			break;
		case 0x0b:
			isChanged |= b.setShima(x, y - 1);
			break;
		case 0x07:
			isChanged |= b.setShima(x - 1, y - 1);
			break;
		case 0x0f:
			throw new HatanException();
		}
		return isChanged;
	}


}
